<?php
/**
 * Created by PhpStorm.
 * User: 曹旺
 * Date: 2018-03-16
 * Time: 09:23
 */

namespace app\index\controller;

use think\Db;
use think\Request;

class Order extends Base
{

    /**
     * @return mixed|string
     * 确认订单前的页面
     * @author  曹旺
     * @data  15:07
     */
    public function generate_order()
    {
        $result = '';
        if ($this->request->isPost()) {
            $key = input('post.sign');
            //要生成的订单商品信息
            //$str = input('post.');
            $data = input('post.commodity/a');
            //用户id
            $user_id = input('post.user_id');
            $address_id = input('post.address_id');
            $user_discount_rate=1;
            if (isset($user_id)){
                $user_discount_rate=$this->user_discount_rate($user_id);
            }
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Order/generate_order';
            $res_pass = $this->encryption($key, $value);
            if (!$res_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => $result];
                return $result_json;
            }
            //$data_array=json_decode($data,true);
            //return json_encode($data);
            $model = new \app\index\model\Order();
            //获取收货地址
            $result_address = $model->generate_order_address($user_id, $address_id);
            //商品信息
            /*$json_result = ['code' => 199, 'msg' => "非法入侵", 'content' =>$user_id.'+'.$address_id];
            return $json_result;*/
            //return $result_address;
            $result = $model->generate_order_info($data, $result_address,$user_discount_rate);
            //return $result;
            $result_json = ['code' => 200, 'msg' => '请求成功', 'content' => $result];
            return $result_json;
        } else {
            $result_json = ['code' => 199, 'msg' => '请求方式错误', 'content' => $result];
            return $result_json;
        }
    }

    /**
     * @return mixed|string
     * 数组：用户id、订单状态
     * 订单
     * @author  曹旺
     * @data  09:52
     */
    public function userorder_info()
    {
        if ($this->request->isPost()) {
            $request = Request::instance();
            $data = $request->except(['sign', 'token']);
            //请求全部订单 order_state=9
            if ($data['order_state'] == 9) {
                unset($data['order_state']);
            }
            if (isset($data['id'])) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            if (!isset($data['user_id'])) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            //验证token
            $token = input('post.token');
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $json_result = ['code' => 199, 'msg' => "非法入侵", 'content' => ''];
                return $json_result;
            }
            $key = input('post.sign');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Order/userorder_info';
            $pass_res = $this->encryption($key, $value);
            if ($pass_res) {
                $model = new \app\index\model\Order();
                $result = $model->user_order($data);
                if (!empty($result)) {
                    $result_json = ['code' => 200, 'msg' => '请求成功', 'content' => $result];
                    return $result_json;
                } else {
                    $result_json = ['code' => -1, 'msg' => '不存在的订单', 'content' => $result];
                    return $result_json;
                }
            } else {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '请求方式错误', 'content' => '管理员正在处理'];
            return $result_json;
        }
    }

    /**
     * @return mixed|string
     *  确认订单
     * @author  曹旺
     * @data  09:38
     */
    public function userorder()
    {
        if ($this->request->isPost()) {
            $user_discount_rate=1;
            $key = input('post.sign');
            $token = input('post.token');
            $data['address_id'] = input('post.address_id');
            $data['user_id'] = input('post.user_id');
            $commodity = input('post.commodity/a');
            //$commodity=[['id'=>'3','number'=>4],['id'=>'5','number'=>6]];
            //return json_encode($commodity_json);
            $data['money'] = input('post.money');
            $datas = input('post.pay_address_way/a');
            if (isset($data['user_id'])){
                $user_discount_rate=$this->user_discount_rate($data['user_id']);
            }
            //return json_encode($data);
            $count = count($datas);
            $pay_address_way = $datas[0];
            for ($i = 0; $i < $count; $i++) {
                $pay_address_way = $pay_address_way . '+' . $datas[$i];
            }
            $data['pay_address_way'] = $pay_address_way;
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Order/userorder';
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $result_json = ['code' => 199, 'msg' => '签名失败', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $result_json = ['code' => 199, 'msg' => '没有登录', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $model = new \app\index\model\Order();
            //验证库存
            //$commodity=json_decode($commodity_json,true);
            $commodity_number = $model->rand_commodity_number($commodity);
            //return json_encode($commodity_number);
            foreach ($commodity_number as $kk => $vv) {
                if ($vv['state'] != 1) {
                    $result_json = ['code' => 199, 'msg' => '商品库存不足', 'content' => $commodity_number];
                    return $result_json;
                }
            }
            //修改商品库存
            $commodity_number = $model->commodity_number($commodity);
            if (!$commodity_number) {
                $result_json = ['code' => 199, 'msg' => '商品库存修改失败', 'content' => $commodity_number];
                return $result_json;
            }
            //生成订单号
            $data['order_number'] = $this->order_number();
            if (!$data['order_number']) {
                $result_json = ['code' => 199, 'msg' => '订单号生成失败', 'content' => '管理员正在处理'];
                return $result_json;
            }
            //生成订单
            $order_id = $model->make_order($data);
            if (!$order_id) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            //添加订单商品
            $order_commodity = $model->make_order_commodity($commodity, $order_id,$user_discount_rate);
            if ($order_commodity) {
                $result_json = ['code' => 200, 'msg' => '请求成功', 'content' => ['order_id'=>$order_id]];
                return $result_json;
            } else {
                $result_json = ['code' => -1, 'msg' => '特殊原因', 'content' => '添加失败'];
                return $result_json;
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
            return $result_json;
        }
    }

    /**
     * @return mixed|string
     * 取消订单
     * @author  曹旺
     * @data  10:41
     */
    public function userorder_cancel()
    {
        if ($this->request->isPost()) {
            $order_id = input('post.id');
            $key = input('post.sign');
            $token = input('post.token');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Order/userorder_cancel';
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $model = new \app\index\model\Order();
            //修改订单状态
            $result = $model->order_cancel($order_id);
            if ($result) {
                $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '删除成功'];
                return $result_json;
            } else {
                $result_json = ['code' => -1, 'msg' => '修改失败', 'content' => '删除失败'];
                return $result_json;
            }
        } else {
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
            return $result_json;
        }
    }

    /**
     * @return mixed|string
     * 确定收货 订单id（修改状态为4）
     * @author  曹旺
     * @data  10:46
     */
    public function userorder_receipt()
    {
        if ($this->request->isPost()) {
            $order_id = input('post.order_id');
            $key = input('post.sign');
            $token = input('post.token');
            $value = 'http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Order/userorder_receipt';
            $sign_pass = $this->encryption($key, $value);
            if (!$sign_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $token_pass = $this->encryption_token($token);
            if (!$token_pass) {
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
                return $result_json;
            }
            $model = new \app\index\model\Order();
            //修改订单状态
            $result = $model->order_receipt($order_id);
            if ($result) {
                $result_json = ['code' => 200, 'msg' => '修改成功', 'content' => '修改成功'];
                return $result_json;
            } else {
                $result_json = ['code' => -1, 'msg' => '修改失败', 'content' => '修改失败'];
                return $result_json;
            }

        } else {
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => '管理员正在处理'];
            return $result_json;
        }
    }


    public function demo()
    {
        /*$arr=Db::table('nbsm_commodity')->field('express_way')->find();
        echo $arr['express_way'];
        $uuu='"[{\"pay_way\":\"\\u51b7\\u94fe\"}]"';
        $aaaa=json_encode($arr['express_way'],true);
        $bbbb=json_decode(json_decode($uuu,true),true);
        dump($aaaa);
        dump($bbbb);die;
        //$arr='[{"id":"4","number":"1"},{"id":"3","number":"5"},{"id":"6","number":"7"}]';
        $arr=input('get.id');
        dump($arr);die;
        $aa=json_decode($arr,true);
        echo $aa[0]['id'];*/
        /*$aee = [];
        $aee[] = 1;
        dump($aee);
        die;*/
        //echo  $arr;die;
        $model = new \app\index\model\Order();
        $a = [['provivce' => '台湾'], ['provivce' => '澳门'], ['provivce' => '香港']];
        $arr=json_encode($a);
        return $arr;
        $aaa = $model->commodity_number($a);
        dump($aaa);
        die;
        //echo $arr ;die;
        $arr = $this->order_number();
        //$arr="0x".$arr;
        dump($arr);
        echo $arr;
        die;
        $n = gmp_init('00000000', 16); //16进制输入
        $n = gmp_add($n, 1); //加1
        echo gmp_strval($n, 16) . '<br/>'; //16进制显示
        $n = gmp_add($n, 13869656); //加法
        echo gmp_strval($n, 16) . '<br/>'; //16进制显示
        die;
        $request = md5(13345678901);
        dump($request);
        die;
        //$data=[['pay_way'=>'冷链'],['pay_way'=>'速运']];
        $data = [['id' => 1, 'number' => 2], ['id' => 2, 'number' => 3]];
        $user_id = 2;
        echo json_encode($data);
        die;
        $model = new \app\index\model\Order();
        $result = $model->generate_order_info($data, $user_id);
        $user_id = 1;
        //$result_address=$model->userorder_address($user_id);
        dump($result);
        //dump($result_address);die;
    }
}